package cn.yuanqiao.manage.service.impl;

import cn.yuanqiao.common.utils.DateUtils;
import cn.yuanqiao.common.utils.SecurityUtils;
import cn.yuanqiao.manage.domain.YqdaCoderuleModel;
import cn.yuanqiao.manage.mapper.YqdaCoderuleModelMapper;
import cn.yuanqiao.manage.service.IYqdaCoderuleModelService;
import cn.yuanqiao.common.core.domain.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * manageService业务层处理
 * 
 * @author yuanqiao
 * @date 2023-06-14
 */
@Service
public class YqdaCoderuleModelServiceImpl implements IYqdaCoderuleModelService 
{
    @Autowired
    private YqdaCoderuleModelMapper yqdaCoderuleModelMapper;

    /**
     * 查询manage
     * 
     * @param Id manage主键
     * @return manage
     */
    @Override
    public YqdaCoderuleModel selectYqdaCoderuleModelById(Long Id)
    {
        return yqdaCoderuleModelMapper.selectYqdaCoderuleModelById(Id);
    }

    /**
     * 查询manage列表
     * 
     * @param yqdaCoderuleModel manage
     * @return manage
     */
    @Override
    public List<YqdaCoderuleModel> selectYqdaCoderuleModelList(YqdaCoderuleModel yqdaCoderuleModel)
    {
        return yqdaCoderuleModelMapper.selectYqdaCoderuleModelList(yqdaCoderuleModel);
    }

    /**
     * 新增manage
     * 
     * @param yqdaCoderuleModel manage
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String insertYqdaCoderuleModel(YqdaCoderuleModel yqdaCoderuleModel) throws ParseException {
        //判断时间冲突
        YqdaCoderuleModel hasyqdaCoderuleModel=new YqdaCoderuleModel();
        hasyqdaCoderuleModel.setCategoryModelId(yqdaCoderuleModel.getCategoryModelId());
        List<YqdaCoderuleModel> yqdaCoderuleModels = yqdaCoderuleModelMapper.selectYqdaCoderuleModelList(hasyqdaCoderuleModel);
        String res="";
        for (int i = 0; i < yqdaCoderuleModels.size(); i++) {

            Date format1 = null;
            format1 = new SimpleDateFormat("yyyyMMdd").parse(String.valueOf(yqdaCoderuleModels.get(i).getStarttime()));
            String starttime = new SimpleDateFormat("yyyyMMdd").format(format1);
            format1 = new SimpleDateFormat("yyyyMMdd").parse(String.valueOf(yqdaCoderuleModels.get(i).getEndtime()));
            String endtime = new SimpleDateFormat("yyyyMMdd").format(format1);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            /**
             * 开始时间在其他范围
             */
            if ((sdf.parse(String.valueOf(yqdaCoderuleModel.getStarttime())).after(sdf.parse(starttime))
                    || sdf.parse(String.valueOf(yqdaCoderuleModel.getStarttime())).equals(sdf.parse(starttime)))
                    && (sdf.parse(String.valueOf(yqdaCoderuleModel.getStarttime())).before(sdf.parse(endtime))
                    || sdf.parse(String.valueOf(yqdaCoderuleModel.getStarttime())).equals(sdf.parse(endtime)))) {
                res = "开始时间与其他规则有重合，请重新选择！";
            }
            /**
             * 结束时间在其他范围
             */
            if ((sdf.parse(String.valueOf(yqdaCoderuleModel.getEndtime())).after(sdf.parse(starttime))
                    || sdf.parse(String.valueOf(yqdaCoderuleModel.getEndtime())).equals(sdf.parse(starttime)))
                    && (sdf.parse(String.valueOf(yqdaCoderuleModel.getEndtime())).before(sdf.parse(endtime))
                    || sdf.parse(String.valueOf(yqdaCoderuleModel.getEndtime())).equals(sdf.parse(endtime)))) {
                res = "结束时间与其他规则有重合，请重新选择！";
            }
            /**
             * 包含了其他时间范围
             */
            if ((sdf.parse(String.valueOf(yqdaCoderuleModel.getStarttime())).before(sdf.parse(starttime))
                    || sdf.parse(String.valueOf(yqdaCoderuleModel.getStarttime())).equals(sdf.parse(starttime)))
                    && (sdf.parse(String.valueOf(yqdaCoderuleModel.getEndtime())).after(sdf.parse(endtime)))
                    || sdf.parse(String.valueOf(yqdaCoderuleModel.getEndtime())).equals(sdf.parse(endtime))) {
                res = "时间中包含其他规则时间范围，请重新选择！";
            }
        }
        if (!"".equals(res)){
            return res;
        }

        LoginUser user = SecurityUtils.getLoginUser();
        if(user == null){
            user = new LoginUser();
            user.setUserId(1L);
        }

        //yqdaCoderuleModel.setCreator(user.getUsername());
        yqdaCoderuleModel.setCreatorId(user.getUserId());
        yqdaCoderuleModel.setCreateTime(DateUtils.getNowDate());
        //判断是否有默认规则了
        if(yqdaCoderuleModel.getIsMoren()==1){
            YqdaCoderuleModel select =new YqdaCoderuleModel();
            select.setIsMoren(1);
            select.setCategoryId(yqdaCoderuleModel.getCategoryId());
            List<YqdaCoderuleModel> yqdaCoderuleModels1 = yqdaCoderuleModelMapper.selectYqdaCoderuleModelList(select);
            if(yqdaCoderuleModels1.size()>0){
                for (int i = 0; i < yqdaCoderuleModels1.size(); i++) {
                    YqdaCoderuleModel listi=yqdaCoderuleModels1.get(i);
                    listi.setIsMoren(0);
                    yqdaCoderuleModelMapper.updateYqdaCoderuleModel(listi);
                }

            }
        }

        int i=yqdaCoderuleModelMapper.insertYqdaCoderuleModel(yqdaCoderuleModel);
        return i>0?"ok":"no";
    }

    /**
     * 修改manage
     * 
     * @param yqdaCoderuleModel manage
     * @return 结果
     */
    @Override
    public String updateYqdaCoderuleModel(YqdaCoderuleModel yqdaCoderuleModel) throws ParseException {
        YqdaCoderuleModel hasyqdaCoderuleModel=new YqdaCoderuleModel();
        hasyqdaCoderuleModel.setId(yqdaCoderuleModel.getId());
        List<YqdaCoderuleModel> yqdaCoderuleModels = yqdaCoderuleModelMapper.selectYqdaCoderuleModelListNotself(hasyqdaCoderuleModel);
        String res="";
        if(null!=yqdaCoderuleModel.getStarttime()&&null!=yqdaCoderuleModel.getEndtime()){
            for (int i = 0; i < yqdaCoderuleModels.size(); i++) {

                Date format1 = null;
                format1 = new SimpleDateFormat("yyyyMMdd").parse(String.valueOf(yqdaCoderuleModels.get(i).getStarttime()));
                String starttime = new SimpleDateFormat("yyyyMMdd").format(format1);
                format1 = new SimpleDateFormat("yyyyMMdd").parse(String.valueOf(yqdaCoderuleModels.get(i).getEndtime()));
                String endtime = new SimpleDateFormat("yyyyMMdd").format(format1);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
                /**
                 * 开始时间在其他范围
                 */
                if ((sdf.parse(String.valueOf(yqdaCoderuleModel.getStarttime())).after(sdf.parse(starttime))
                        || sdf.parse(String.valueOf(yqdaCoderuleModel.getStarttime())).equals(sdf.parse(starttime)))
                        && (sdf.parse(String.valueOf(yqdaCoderuleModel.getStarttime())).before(sdf.parse(endtime))
                        || sdf.parse(String.valueOf(yqdaCoderuleModel.getStarttime())).equals(sdf.parse(endtime)))) {
                    res = "开始时间与其他规则有重合，请重新选择！";
                }
                /**
                 * 结束时间在其他范围
                 */
                if ((sdf.parse(String.valueOf(yqdaCoderuleModel.getEndtime())).after(sdf.parse(starttime))
                        || sdf.parse(String.valueOf(yqdaCoderuleModel.getEndtime())).equals(sdf.parse(starttime)))
                        && (sdf.parse(String.valueOf(yqdaCoderuleModel.getEndtime())).before(sdf.parse(endtime))
                        || sdf.parse(String.valueOf(yqdaCoderuleModel.getEndtime())).equals(sdf.parse(endtime)))) {
                    res = "结束时间与其他规则有重合，请重新选择！";
                }
                /**
                 * 包含了其他时间范围
                 */
                if ((sdf.parse(String.valueOf(yqdaCoderuleModel.getStarttime())).before(sdf.parse(starttime))
                        || sdf.parse(String.valueOf(yqdaCoderuleModel.getStarttime())).equals(sdf.parse(starttime)))
                        && (sdf.parse(String.valueOf(yqdaCoderuleModel.getEndtime())).after(sdf.parse(endtime)))
                        || sdf.parse(String.valueOf(yqdaCoderuleModel.getEndtime())).equals(sdf.parse(endtime))) {
                    res = "时间中包含其他规则时间范围，请重新选择！";
                }
            }
            if (!"".equals(res)){
                return res;
            }
        }
        //判断是否有默认规则了
        if(yqdaCoderuleModel.getIsMoren()==1){
            YqdaCoderuleModel select =new YqdaCoderuleModel();
            select.setIsMoren(1);
            select.setCategoryModelId(yqdaCoderuleModelMapper.getcateidbyid(yqdaCoderuleModel.getId()));
            List<YqdaCoderuleModel> yqdaCoderuleModels1 = yqdaCoderuleModelMapper.selectYqdaCoderuleModelList(select);
            if(yqdaCoderuleModels1.size()>0){
                for (int i = 0; i < yqdaCoderuleModels1.size(); i++) {
                    YqdaCoderuleModel listi=yqdaCoderuleModels1.get(i);
                    listi.setIsMoren(0);
                    yqdaCoderuleModelMapper.updateYqdaCoderuleModel(listi);
                }

            }
        }
        int i=yqdaCoderuleModelMapper.updateYqdaCoderuleModel(yqdaCoderuleModel);
        return i>0?"ok":"no";
    }

    /**
     * 批量删除manage
     * 
     * @param Ids 需要删除的manage主键
     * @return 结果
     */
    @Override
    public int deleteYqdaCoderuleModelByIds(Long[] Ids)
    {
        return yqdaCoderuleModelMapper.deleteYqdaCoderuleModelByIds(Ids);
    }

    /**
     * 删除manage信息
     * 
     * @param Id manage主键
     * @return 结果
     */
    @Override
    public int deleteYqdaCoderuleModelById(Long Id)
    {
        return yqdaCoderuleModelMapper.deleteYqdaCoderuleModelById(Id);
    }
}
